import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:go_pet/src/features/login/view/widgets/components/my_button.dart';
import 'package:go_pet/src/theme/app_colors.dart';
import 'package:url_launcher/url_launcher_string.dart';

class PetPage extends StatefulWidget {
  final String title;
  final String description;
  final String imageUrl;
  final String time;
  final String tgId;

  const PetPage({
    super.key,
    required this.title,
    required this.description,
    required this.imageUrl,
    required this.time,
    required this.tgId,
  });

  @override
  State<PetPage> createState() => _PetPageState();
}

class _PetPageState extends State<PetPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SafeArea(
        child: Container(
          color: AppColors.white,
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            child: Column(
              children: [
                CachedNetworkImage(
                  height: 300,
                  imageUrl: widget.imageUrl,
                  errorWidget: (context, url, error) => const Icon(
                    Icons.error,
                    size: 50,
                    color: Colors.red,
                  ),
                  progressIndicatorBuilder: (context, url, downloadProgress) =>
                      Center(
                    child: SizedBox(
                      width: 50,
                      height: 50,
                      child: CircularProgressIndicator(
                        value: downloadProgress.progress,
                        color: AppColors.primary,
                      ),
                    ),
                  ),
                ),
                Row(
                  children: [
                    Flexible(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            widget.title,
                            style: Theme.of(context).textTheme.titleLarge,
                          ),
                          Text(
                            softWrap: true,
                            widget.description,
                            style: const TextStyle(
                              fontSize: 20,
                              color: Color.fromARGB(255, 24, 24, 24),
                            ),
                          ),
                          const SizedBox(
                            height: 10,
                          ),
                          Text(
                            'tg:${widget.tgId}',
                            style: const TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 20,
                              color: Color.fromARGB(255, 24, 24, 24),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
      floatingActionButton: SizedBox(
        width: 380,
        child: MyButton(
          text: "Написать",
          onTap: () async {
            try {
              await launchUrlString('https://t.me/${widget.tgId.substring(1)}');
            } catch (err) {
              debugPrint('Something bad happened');
            }
          },
        ),
      ),
    );
  }
}
